package com.cdshangpai.photoapp;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.cdshangpai.photoapp.databinding.ActivityMainBinding;
import com.cdshangpai.photoapp.ui.main.LoginFragment;
import com.cdshangpai.photoapp.ui.main.MainFragment;
import com.cdshangpai.utils.system.ScreenParams;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends PhotoAppBaseActivity {
    ActivityMainBinding binding;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    LoginFragment loginFragment;
    MainFragment mainFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPermissions();
        binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
        binding.setView(this);

        initFragment();
        switchFragment();
        hideNav();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ScreenParams.pickScreenParams();
        switchFragment();
    }

    private void hideNav()
    {
        View view=getWindow().getDecorView();
        view.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
    

    /**
     * 初始化页面片段
     */
    private void initFragment()
    {
        loginFragment=new LoginFragment();
        mainFragment=new MainFragment();

        fragmentManager=getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.main_contaner,loginFragment).add(R.id.main_contaner,mainFragment).commit();
    }

    /**
     * 根据登录状态自动显示登录页面或主页
     */
    public void switchFragment()
    {
        Boolean logined=LoginFragment.logined();
        logined=true;
        fragmentTransaction=fragmentManager.beginTransaction();
        if(!logined)
        {
            fragmentTransaction.hide(mainFragment).show(loginFragment).commit();
        }
        else
        {
            fragmentTransaction.hide(loginFragment).show(mainFragment).commit();
            mainFragment.setAccountName();
        }
    }

    /**
     * 自动授权
     */
    private void getPermissions()
    {
        String[] perms={Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.MANAGE_EXTERNAL_STORAGE,
                Manifest.permission.REQUEST_INSTALL_PACKAGES,
                Manifest.permission.CALL_PHONE};
        if(!EasyPermissions.hasPermissions(this,perms)){
            Log.i(LogConfig.logTag,"缺少权限，提示用户操作");
            EasyPermissions.requestPermissions(this,"请授予权限，否则系统无法正常使用。",10001,perms);
        }
        else
        {
            Log.i(LogConfig.logTag,"已经拥有全部权限");
        }
    }
}